from setting import *


class Menu(pygame.sprite.Sprite):
    """ display a menu background"""

    def __init__(self):
        self.groups = allgroup, menugroup
        self._layer = -2
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.image.load('images/bg.jpg').convert_alpha()
        self.rect = self.image.get_rect()

    def update(self):
        pass


class Text(pygame.sprite.Sprite):
    """ display a text"""

    def __init__(self, msg):
        self.groups = allgroup, textgroup
        self._layer = 99
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.newmsg(msg)

    def write(self, msg="pygame is cool"):
        """write text into pygame surfaces"""
        myfont = pygame.font.SysFont("None", 32)
        mytext = myfont.render(msg, True, (0, 255, 0))
        mytext = mytext.convert_alpha()
        return mytext

    def update(self):
        pass

    def newmsg(self, msg="i have nothing to say"):
        self.image = self.write(msg)
        self.rect = self.image.get_rect()
        self.rect.center = (SCENEWIDTH // 2, 10)


class Background(pygame.sprite.Sprite):
    """
        generate a Background sprite for the background, 
           to demonstrate vertical scrolling.background slide from top to bottom
           parameter: vect_pos must use in pygame.math.Vector2D type
    """

    def __init__(self, vect_pos):
        self._layer = -1
        self.groups = allgroup, backgroundgroup
        pygame.sprite.Sprite.__init__(self, self.groups)  # THE Line
        self.image = pygame.image.load('images/bg/bg.png').convert_alpha()
        # self.image.convert_alpha()
        self.rect = self.image.get_rect()
        self.pos = vect_pos
        self.rect.x = self.pos.x
        self.rect.y = self.pos.y
        self.parent = False

    def update(self):
        self.rect.y += 1
        if self.rect.y > SCENEHEIGHT:
            #kill了以后，还是能继续下面的print（）的。
            self.kill()
        # create new background if necessary
        # 下面这段代码真是精彩，背景图有两张,例如bg1,bg2，均默认parent= Flase
        # bg1需要加第二张bg2时(self.rect.y>=0)时，第一张bg1的parent = Ture,此时还没达到bg1的kill的判定
        # 能继续运行，但不能再产生新图。
        # 新生成的背景图bg2，默认parent = False，代替了第一张bg1的功能，进行下面的代码的判定，条件满足时
        # 能继续产生新背景图bg3，产生bg3后，bg2的parent= True,不能在产生新图，bg3的parent=Falses ……
        # 可以用print(self.parent,self.rect.y)来查看运行状态，同时存在两个图片
        # print(self.parent, self.rect.y)
        if not self.parent:
            if self.rect.y >= 0:
                self.parent = True
                Background(vect(0, -SCENEHEIGHT))
